/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.core.analyzer;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.open.cobertura.core.instrument.CoberturaClassProbeVisitor;
import org.open.cobertura.core.instrument.CoberturaMethodProbeVisitor;
import org.open.cobertura.core.model.InstructionStateful;
import org.open.cobertura.util.JavaNamesUtils;

import java.util.Map;

/**
 * 类的分析
 */
public class CoberturaClassAnalyzer extends CoberturaClassProbeVisitor {
    /**
     * 类名
     */
    private String name;
    private final boolean[] probes;
    private final CoberturaClassCoveredImpl coberturaClassCovered;

    protected CoberturaClassAnalyzer(final ClassVisitor classVisitor,final boolean[] probes,final CoberturaClassCoveredImpl coberturaClassCovered) {
        super(Opcodes.ASM9,classVisitor);
        this.probes=probes;
        this.coberturaClassCovered=coberturaClassCovered;
    }


    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        super.visit(version, access, name, signature, superName, interfaces);
        this.name=name;
        JavaNamesUtils javaNamesUtils=new JavaNamesUtils();
        String methodName=javaNamesUtils.getClassName(this.name,name,superName,interfaces);
        String packageName=javaNamesUtils.getPackageName(name.substring(0,name.lastIndexOf("/")));
        coberturaClassCovered.setName(methodName);
        coberturaClassCovered.setPackageName(packageName);
    }

    @Override
    public CoberturaMethodProbeVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions,long classId) {
        final CoberturaInstructionFactory builder = new CoberturaInstructionFactory(probes);
        return new CoberturaMethodAnalyzer(builder){
            /**
             * 计算方法覆盖率。
             * @param methodNode 方法节点
             * @param methodVisitor 方法访问器
             */
            @Override
            public void accept(final MethodNode methodNode, final MethodVisitor methodVisitor) {
                super.accept(methodNode, methodVisitor);
                //这里添加计算覆盖率的方法
                calculateMethodCoverage(name,descriptor,signature,builder,methodNode);
            }
        };
    }

    /**
     * 计算方法覆盖率
     * @param name
     * @param desc
     * @param signature
     * @param cif
     * @param methodNode
     */
    private void calculateMethodCoverage(final String name, final String desc,
                                        final String signature, final CoberturaInstructionFactory cif,
                                        final MethodNode methodNode){
        final Map<AbstractInsnNode,InstructionStateful> map=cif.getInstructionStatefulMap();
        JavaNamesUtils javaNamesUtils=new JavaNamesUtils();
        String methodName=javaNamesUtils.getMethodName(this.name,name,desc,signature);
        final CoberturaMethodCoveredImpl coberturaMethodCovered=new CoberturaMethodCoveredImpl(methodName);
        for (final Map.Entry<AbstractInsnNode, InstructionStateful> entry : map.entrySet()) {
            final InstructionStateful instruction = entry.getValue();
            coberturaMethodCovered.increase(instruction.getInstructionCount(),instruction.getBranchCount(),instruction.getLineNumber());
        }
        coberturaMethodCovered.increaseMethodCovered();
        coberturaClassCovered.increase(coberturaMethodCovered);
    }
}
